home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVBJ10.C < prev    next >
C/C++ Source or Header  |  1992-03-16  |  5KB  |  168 lines

  1. /* Copyright (C) 1990, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevbj10.c */
  21. /* Canon Bubble Jet BJ-10e printer driver for Ghostscript */
  22. #include "gdevprn.h"
  23.  
  24. /*
  25.  * The only available resolutions are (180,360)x(180,360).
  26.  */
  27.  
  28. /* The device descriptor */
  29. private dev_proc_print_page(bj10e_print_page);
  30. gx_device_printer gs_bj10e_device =
  31.   prn_device(prn_std_procs, "bj10e",
  32.     80,                /* width_10ths, 8" */
  33.     105,                /* height_10ths, 10.5" */
  34.     360,                /* x_dpi */
  35.     360,                /* y_dpi */
  36.     0,0,0,0,            /* margins */
  37.     1, bj10e_print_page);
  38.  
  39. /* ------ internal routines ------ */
  40.  
  41. /* Send the page to the printer. */
  42. private int
  43. bj10e_print_page(gx_device_printer *pdev, FILE *prn_stream)
  44. {    int line_size = gx_device_bytes_per_scan_line((gx_device *)pdev, 1);
  45.     int xres = pdev->x_pixels_per_inch;
  46.     int yres = pdev->y_pixels_per_inch;
  47.     int mode = (yres == 180 ?
  48.             (xres == 180 ? 11 : 12) :
  49.             (xres == 180 ? 14 : 16));
  50.     int bits_per_column = 24 * (yres / 180);
  51.     int bytes_per_column = bits_per_column / 8;
  52.     int skip_unit = 9 * (xres / 180);
  53.     byte *in = (byte *)gs_malloc(8, line_size, "bj10e_print_page(in)");
  54.     byte *out = (byte *)gs_malloc(bits_per_column, line_size, "bj10e_print_page(out)");
  55.     static char cmp[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  56.     int lnum = 0;
  57.     int skip = 0;
  58.     int code = 0;
  59.  
  60.     if ( in == 0 || out == 0 )
  61.         return -1;
  62.  
  63.     /* Initialize the printer. */
  64.     fwrite("\033[K\004\000\000\044\000\000", 1, 9, prn_stream);
  65.  
  66.     /* Set vertical spacing. */
  67.     fwrite("\033[\\\004\000\000\000", 1, 7, prn_stream);
  68.     fputc(yres & 0xff, prn_stream);
  69.     fputc(yres >> 8, prn_stream);
  70.  
  71.     /* Transfer pixels to printer */
  72.     while ( lnum < pdev->height )
  73.        {    byte *in_end = in + line_size;
  74.         byte *out_beg = out;
  75.         byte *out_end = out + bytes_per_column * pdev->width;
  76.         byte *outl = out;
  77.         int count, bnum;
  78.  
  79.         /* Copy 1 scan line and test for all zero. */
  80.         code = gdev_prn_get_bits(pdev, lnum, in, line_size, -1);
  81.         if ( code < 0 ) goto xit;
  82.         /* The mem... or str... functions should be faster than */
  83.         /* the following code, but all systems seem to implement */
  84.         /* them so badly that this code is faster. */
  85.            {    register long *zip = (long *)in;
  86.             register int zcnt = line_size;
  87.             static long zeroes[4] = { 0, 0, 0, 0 };
  88.             for ( ; zcnt >= 4 * sizeof(long); zip += 4, zcnt -= 4 * sizeof(long) )
  89.                {    if ( zip[0] | zip[1] | zip[2] | zip[3] )
  90.                     goto notz;
  91.                }
  92.             if ( !memcmp(in, (char *)zeroes, zcnt) )
  93.                {    /* Line is all zero, skip */
  94.                 lnum++;
  95.                 skip++;
  96.                 continue;
  97.                }
  98. notz:            ;
  99.            }
  100.  
  101.         /* Vertical tab to the appropriate position. */
  102.         while ( skip > 255 )
  103.            {    fputs("\033J\377", prn_stream);
  104.             skip -= 255;
  105.            }
  106.         if ( skip )
  107.             fprintf(prn_stream, "\033J%c", skip);
  108.  
  109.         /* Transpose in blocks of 8 scan lines. */
  110.         for ( bnum = 0; bnum < bits_per_column; bnum += 8, lnum += 8 )
  111.            {    int lcnt = gdev_prn_copy_scan_lines(pdev,
  112.                 lnum, in, 8 * line_size);
  113.             byte *inp = in;
  114.             byte *outp = outl;
  115.             if ( lcnt < 0 )
  116.                {    code = lcnt;
  117.                 goto xit;
  118.                }
  119.             if ( lcnt < 8 )
  120.                 memset(in + lcnt * line_size, 0,
  121.                        (8 - lcnt) * line_size);
  122.             for ( ; inp < in_end; inp++, outp += bits_per_column )
  123.                {    gdev_prn_transpose_8x8(inp, line_size,
  124.                     outp, bytes_per_column);
  125.                }
  126.             outl++;
  127.            }
  128.  
  129.         /* Remove trailing 0s. */
  130.         while ( out_end - 6 >= out )
  131.            {    if ( out_end[-1] | out_end[-2] | out_end[-3] |
  132.                  out_end[-4] | out_end[-5] | out_end[-6]
  133.                )
  134.                 break;
  135.             out_end -= 6;
  136.            }
  137.  
  138.         /* Remove leading 0s. */
  139.         while ( out_beg + skip_unit <= out_end )
  140.            {    if( memcmp(cmp, (char *)out_beg, skip_unit) != 0 )
  141.                 break;
  142.             out_beg += skip_unit;
  143.            }
  144.  
  145.         /* Transfer the bits */
  146.         count = out_end - out_beg + 1;
  147.         if ( out_beg > out && count > 1 )
  148.            {    int skip = (out_beg - out) / skip_unit;
  149.             if ( xres == 180 ) skip <<= 1;
  150.             fprintf(prn_stream, "\033d%c%c",
  151.                 skip & 0xff, skip >> 8);
  152.            }
  153.         fprintf(prn_stream, "\033[g%c%c%c",
  154.             count & 0xff, count >> 8, mode);
  155.         fwrite(out_beg, 1, count - 1, prn_stream);
  156.         fputc('\r', prn_stream);
  157.         skip = bits_per_column;
  158.        }
  159.  
  160.     /* Eject the page */
  161. xit:    fputc(014, prn_stream);    /* form feed */
  162.     fflush(prn_stream);
  163.  
  164.     gs_free((char *)in, 8, line_size, "bj10e_print_page(in)");
  165.     gs_free((char *)out, bits_per_column, line_size, "bj10e_print_page(out)");
  166.     return code;
  167. }
  168.